package com.mia.props.common.entities;

import com.mia.props.common.TileProps;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemRecord;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class TileRecordPlayer extends TileProps {
   private ItemStack record;

   public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float var7, float var8, float var9) {
      if (!world.isRemote) {
         ItemStack itemStack = player.getHeldItem();
         if (itemStack != null && itemStack.getItem() instanceof ItemRecord) {
            if (this.record != null) {
               this.dropRecord(world, x, y, z);
            }

            this.record = itemStack.copy();
            this.record.stackSize = 1;
            --itemStack.stackSize;
            world.playAuxSFXAtEntity((EntityPlayer)null, 1005, x, y, z, Item.getIdFromItem(itemStack.getItem()));
         }

         if (itemStack == null && this.record != null) {
            this.dropRecord(world, x, y, z);
         }
      }

      return false;
   }

   public void onBlockHarvested(World world, int x, int y, int z, int par5, EntityPlayer par6EntityPlayer) {
      if (!world.isRemote && this.record != null) {
         this.dropRecord(world, x, y, z);
      }

   }

   private void dropRecord(World world, int x, int y, int z) {
      if (!world.isRemote) {
         ItemStack itemStack = this.record.copy();
         this.record = null;
         world.playAuxSFX(1010, x, y, z, 0);
         world.playRecord((String)null, x, y, z);
         float f = 0.7F;
         double d0 = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
         double d1 = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.2D + 0.6D;
         double d2 = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
         EntityItem entityitem = new EntityItem(world, (double)x + d0, (double)y + d1, (double)z + d2, itemStack);
         entityitem.delayBeforeCanPickup = 10;
         world.spawnEntityInWorld(entityitem);
      }

   }
}
